package com.nursinghome.admin.controller;

import com.nursinghome.bean.Goods;
import com.nursinghome.bean.RespBean;
import com.nursinghome.common.service.GoodsService;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/admin/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    //获取到所有的商品
    @GetMapping
    public RespBean selectAll(String pageNum, String pageSize, Goods goods) {

        return RespBean.ok("",goodsService.selectAll(pageNum, pageSize, goods));
    }
    //新增商品
    @PostMapping
    public RespBean insert(@RequestBody Goods goods) throws NurisingHomeException {
        if (goodsService.insert(goods)) {
            return RespBean.ok("新增成功");
        }
        return RespBean.error("新增失败");
    }
    //修改商品
    @PutMapping
    public RespBean update(@RequestBody Goods goods) throws NurisingHomeException {
        if (goodsService.update(goods)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    //删除商品
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id) throws NurisingHomeException {
        if (goodsService.delete(id)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //回显商品数据
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id) throws NurisingHomeException {
        Goods goods = goodsService.selectById(id);
        return RespBean.ok("",goods);
    }
}
